<?php
require_once("header.php");
require_once("model/DB.php");

require_once("model/User.php");
require_once("model/Msg.php");

require_once("function/teacher.login.php");

$msgs = Msg::getMsgs($_SESSION['userid']);

?>
<script>
function reply(id){
	var check_id = 0;
	$("#add_form").children().each(function(i){
		if($(this).children().find('.to_id').val()==id)
		{	
			
			check_id++;
			
		}
	});
	if(check_id>0){
		return false;
	}

			
			var div = $("#kid_form").clone(true);
			
			div.css("display","block");
			
			div.find(".to_id").val(id);	
			div.appendTo($("#add_form"));



}


$(document).ready(function(){
	$("#kid_table").tablesorter( 
		{widthFixed: true, widgets: ['zebra']} 
	).tablesorterPager({container: $("#pager")});
	
	
	/*$("#add").click(function(){
		var p_id = 0;		
		$("#add_form").children().each(function(i){
			p_id++;
		});		
		
		var div = $("#kid_form").clone(true);
		div.find("form").find(".file").attr("id",p_id+"_image");
		div.css("display","block");
		div.appendTo($("#add_form"));
	});*/
	
	$(".cancel").click(function(){
		
		$(this).parents().each(function(i){
			if(i==4){
				$(this).remove();
			}
		});
	});	
	
	$(".add_kid").submit(function(){
		form = $(this);
		var msg = $(this).find(".content").val();
		var to_id = $(this).find(".to_id").val();

		
		$.post("function/addMsg.php",
				   {msg:msg,to_id:to_id},
				   function(data){
					   if(data=="saved"){
						   alert("Message Has Been Sent");
						   $("#kid_list").load("pieces/msglist.php");
					   }else{
						   alert(data);
					   }
				   }
					);
		return false;
		
	});
});



</script>
<center>
<table id="admin_detail" cellspacing="0" cellpadding="0" >
<tr>
<td id="left_bar" style="border-right:1px solid black;text-align:center; width:100px;">

<br>
<a class="admin_left" href="teacher.php">Home</a>
<br>
<br>
<!--  <a class="admin_left" href="javascript:void(0)" id="add">Add Achivement</a> -->
</td>
<td>
<div id="kid_list">
<table id="kid_table" class="tablesorter">
	<thead>
		<tr><td>From</td><td>Content</td><td>Datetime</td><td>Action</td></tr>
	</thead>
	<tbody>
	<tr></tr>
		<?php
		if($msgs){
			
			foreach($msgs as $msg){
				$user = User::getUser('id',$msg['from_id']);
				$to = User::getUser('id',$msg['to_id']);
				?>
				<tr><td><?php if($msg['from_id']!=$_SESSION['userid']){ echo $user[0]['username'];}else{ echo "You to <font color='red'>".$to[0]['username']."</font>";  }?></td><td><?=$msg['content']?></td><td><?=$msg['datetime']?></td><td><?php if($msg['from_id']!=$_SESSION['userid']): ?><a href="javascript:void(0)" onclick="reply(<?=$msg['from_id']?>)">Reply</a><?php endif; ?></td></tr>
				<?php
			}
		}
		?>
	</tbody>
</table>


<div id="pager" class="pager" style="width:800px;margin-left:5px;margin-top:15px">
					<form>
						<img class="first" src="images/first.png"/>
						<img class="prev" src="images/prev.png"/>
						<input class="pagedisplay" type="text"/>
						<img class="next" src="images/next.png"/>
						<img class="last" src="images/last.png"/>
						<select id="pagesize" class="pagesize">
							 <option value="10" >10</option>
							 <option value="15" >15</option>
							 
						</select>
					</form>
</div>
</div>
<div id="kid_form" style="display:none">
<form class="add_kid">
<table>
	<tr>
		<td>Content</td><td><textarea class="content"></textarea><input type="hidden" class="to_id"></td>
	</tr>
	

	<tr>
		<td><input type="submit" value="Send"></td><td><input type="button" class="cancel" value="Cancel"></td>
	</tr>
</table>
</form>
</div>
<div id="add_form">
</div>
</td>
</tr>
</table>
</center>